﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace QuanLyThuVien
{
    public partial class ThanhLySach : Form
    {
        public ThanhLySach()
        {
            InitializeComponent();
        }

        void LoadDSThanhLySach()
        {
            DataTable dt = ThanhLySachBUS.LayDanhSachThanhLySach_BUS();
            dataGridViewX1.DataSource = dt;
            dataGridViewX1.Columns["LyDo"].Visible = false;
            dataGridViewX1.Columns["MaThanhLySach"].HeaderText = "Mã thanh lý";
            dataGridViewX1.Columns["MaSach"].HeaderText = "Mã sách";
            dataGridViewX1.Columns["TenSach"].HeaderText = "Tên sách";
            dataGridViewX1.Columns["TenLyDo"].HeaderText = "Lý do";
            dataGridViewX1.Columns["NgayThanhLy"].HeaderText = "Ngày thanh lý";
            dataGridViewX1.Columns["HoTen"].HeaderText = "Họ tên người thanh lý";

        }
        void LoadDSSach()
        {
            DataTable dt = SachBUS.LayDanhSachSach_BUS();
            cbbmasach.DataSource = dt;
            cbbmasach.ValueMember = "MaSach";
            cbbmasach.DisplayMember = "TenSach";
        }
        void LoadDSLyDoTL()
        {
            DataTable dt = LyDoThanhLyBUS.LayDSLyDoThanhLy();
            cbblydothanhly.DataSource = dt;
            cbblydothanhly.ValueMember = "MaThanhLy";
            cbblydothanhly.DisplayMember = "TenLyDo";
        }
        void LoadDSNhanVien()
        {
            List<NhanVienDTO> dt = NhanVienBUS.laydanhsachnhanvien_bus();
            cbbmanhanvien.DataSource = dt;
            cbbmanhanvien.ValueMember = "MaNhanVien";
            cbbmanhanvien.DisplayMember = "HoTen";
        }
        private void ThanhLySach_Load(object sender, EventArgs e)
        {
            LoadDSThanhLySach();
            LoadDSSach();
            LoadDSLyDoTL();
            LoadDSNhanVien();
        }

        private void btlammoi_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in groupPanel1.Controls)
            {
                if (ctrl.GetType() != typeof(DevComponents.DotNetBar.ButtonX) && ctrl.GetType() != typeof(DevComponents.DotNetBar.LabelX))
                {
                    ctrl.ResetText();
                }
            }
        }

        private void btlapphieu_Click(object sender, EventArgs e)
        {
            ThanhLySachDTO tlsDTO = new ThanhLySachDTO();
            tlsDTO.Ngaythanhly = dtngaythanhly.Value;
            tlsDTO.Nguoithanhly=int.Parse(cbbmanhanvien.SelectedValue.ToString());
            tlsDTO.ChiTiet = new ChiTietThanhLyDTO();
            tlsDTO.ChiTiet.Lydo = int.Parse(cbblydothanhly.SelectedValue.ToString());
            tlsDTO.ChiTiet.Masach = int.Parse(cbbmasach.SelectedValue.ToString());
            tlsDTO.ChiTiet.Mathanhly = ThanhLySachBUS.LayThanhLySachCuoiCung_BUS() + 1;
            ThanhLySachBUS.ThemPhieuThanhLy_BUS(tlsDTO);
            LoadDSThanhLySach();
            btlammoi_Click(null, null);
        }

        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX1.SelectedRows.Count > 0 && dataGridViewX1.SelectedRows[0].IsNewRow == false)
            {
                
                DataGridViewRow dr=dataGridViewX1.SelectedRows[0];
                cbbmanhanvien.SelectedValue= int.Parse(dr.Cells["NguoiThanhLy"].Value.ToString());
                cbbmasach.SelectedValue = int.Parse(dr.Cells["MaSach"].Value.ToString());
                cbblydothanhly.SelectedValue = int.Parse(dr.Cells["LyDo"].Value.ToString());
                dtngaythanhly.Value = DateTime.Parse(dr.Cells["NgayThanhLy"].Value.ToString());
                maThanhLy = int.Parse(dr.Cells["MaThanhLySach"].Value.ToString());
            }
        }
        int maThanhLy = -1;
        private void btxoaphieu_Click(object sender, EventArgs e)
        {
            ThanhLySachBUS.XoaThanhLySach(maThanhLy);
            LoadDSThanhLySach();
        }

        private void btcapnhat_Click(object sender, EventArgs e)
        {
            ThanhLySachDTO tlsDTO = new ThanhLySachDTO();
            tlsDTO.Mathanhlysach = maThanhLy;
            tlsDTO.Ngaythanhly = dtngaythanhly.Value;
            tlsDTO.Nguoithanhly = int.Parse(cbbmanhanvien.SelectedValue.ToString());
            tlsDTO.ChiTiet = new ChiTietThanhLyDTO();
            tlsDTO.ChiTiet.Lydo = int.Parse(cbblydothanhly.SelectedValue.ToString());
            tlsDTO.ChiTiet.Masach = int.Parse(cbbmasach.SelectedValue.ToString());
            tlsDTO.ChiTiet.Mathanhly = maThanhLy;
            ThanhLySachBUS.CapNhatThanhLySach_BUS(tlsDTO);
            LoadDSThanhLySach();
            

        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttimphieu_Click(object sender, EventArgs e)
        {
            int ms = int.Parse(cbbmasach.SelectedValue.ToString());
            if (ms > -1)
            {
                DataTable dt = ThanhLySachBUS.TimKiemThanhLySach_BUS(ms);
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.Columns["LyDo"].Visible = false;
                dataGridViewX1.Columns["MaThanhLySach"].HeaderText = "Mã thanh lý";
                dataGridViewX1.Columns["MaSach"].HeaderText = "Mã sách";
                dataGridViewX1.Columns["TenSach"].HeaderText = "Tên sách";
                dataGridViewX1.Columns["TenLyDo"].HeaderText = "Lý do";
                dataGridViewX1.Columns["NgayThanhLy"].HeaderText = "Ngày thanh lý";
                dataGridViewX1.Columns["HoTen"].HeaderText = "Họ tên người thanh lý";
            }

        }

        

       

        

        
    }
}